<template>
  <div class="right-header">
    <div class="welcome" style="">欢迎你，管理员!</div>
    <div class="profile">
      <el-dropdown trigger="click">
        <span class="el-dropdown-link">
          <img class="avatar" :src="avatar" alt="">
          <span class="name">{{username}}</span>
          <i class="el-icon-caret-bottom el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown" >
          <el-dropdown-item @click.native="() => dialogPassword = true">修改密码</el-dropdown-item>
          <el-dropdown-item divided @click.native="go()">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-dialog  title="修改密码" :visible.sync="dialogPassword"  v-if="dialogPassword" custom-class="dialog-platform" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" width="460">
        <el-form-item class="input" label="账户名称">
          <el-input v-model="username" readonly></el-input>
        </el-form-item>
        <el-form-item label="原密码" prop="oldpass">
          <el-input type="password" v-model="ruleForm.oldpass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogPassword = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="ifLoadding">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
  import avatar from 'common/images/avatar2.png'
  import{BackToLogin,getToken} from '../../common/js/uilt'
  export default {
    data () {
      var validateOld = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入原密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else if (String(value).length < 6 || String(value).length > 20) {
          callback(new Error('只能输入6-20位的密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        avatar: avatar,
        dialogPassword: false,
        ifLoadding: false,
        username: '',
        ruleForm: {
          oldpass: '',
          pass: '',
          checkPass: ''
        },
        rules: {
          oldpass: [
            { validator: validateOld, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      go(){
        this.$confirm('此操作将退出登录, 是否继续?', '系统温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '成功退出!'
          });
          sessionStorage.removeItem('token');
          this.$router.push({path:'/login'});
        })
      },
      _initInfo(){
        const perfix = 'pj-p2p-user';
        let that = this;
        this.$axios.post(`/${perfix}/admin/info/get`,getToken()).then(function (res) {
            if(res.data.data.username==""){
                return
            }else {
                that.username = res.data.data.username;
                sessionStorage.setItem('ADMIN_NAME', res.data.data.username)
            }
        }).catch(function (ref) {

        })
      },
      // 修改密码
      submitForm(formName) {
        if (this.ifLoadding) return
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = {
              oldPassword: this.ruleForm.oldpass,
              password: this.ruleForm.pass
            }
            this.ifLoadding = true
            this.$api.adminUserUpdate(params).then(rs => {
              if (rs.retCode === this.$api.STATUS_OK) {
                this.dialogPassword = false
                this.$message({ type: 'success',message: '密码修改成功' });
              } else {
                this.$message({ type: 'error',message: rs.retMsg });
              }
              this.ifLoadding = false
            })
          }
        });
      }
    },
  created(){
    this._initInfo();
  },
  watch: {
    dialogPassword (val, oldVal) {
      if (val) {
        this.ruleForm = {
          oldpass: '',
          pass: '',
          checkPass: ''
        }
        if (this.$refs['ruleForm']) {
          this.$refs['ruleForm'].resetFields();
        }
      }
    }
  }
}
</script>

<style scoped lang="stylus">
.right-header {
  height: 50px;
  padding-left: 200px;
  background: #eef1f6;
  .welcome {
    float: left;
    line-height: 50px;
    padding-left: 20px;
  }
  .profile {
    float: right;
    height: 50px;
    line-height: 50px;
    padding-left: 20px;
    background: #D3DCE6;
    color: #fff;
    >>> .el-dropdown {
      display: inline-block;
      height: 50px;
      line-height: 50px;
      margin-right: 20px;
      cursor: pointer;
    }
    .avatar {
      width: 30px;
      height: 30px;
      vertical-align: top;
      margin-top: 10px;
    }
    .name {
      display: inline-block;
      height: 50px;
      line-height: 50px;
      margin: 0 5px 0 10px;
      font-size: 18px;
      color: #475669;
    }
    .el-icon-caret-bottom {
      margin-top: 18px;
      vertical-align: top;
      color: #475669;
    }
  }
}
</style>
